<?php

/**
 * 用户相关接口
 *
 */
class ApiUserAction extends ApiBasic
{
    //固定手机号登录,手机号=>用户id
    protected $loginPhone = array(
        '13560432198' => 100001,
//         '15917375440' => 100002,//测试
//         '19925856546' => 100003,//测试
        '18820002564' => 100004,
        '13543405600' => 100005,
        '18688891195' => 100006,
        '19924292507' => 100007,
        '13022084075' => 100008,
        '15099866552' => 100009,
        '13760607918' => 100010,
        '13702220670' => 100011,
        '13580451676' => 100012,//启明
    );

    protected $cmdCollect = array(
        10001 => array('method' => 'smsLogin', 'login' => false, 'auth' => false, 'auth_key' => "sms_login"),
        10002 => array('method' => 'loginSendSms', 'login' => false, 'auth' => false, 'auth_key' => "login_send_sms"),
    );

    /**
     * [accountLogin 账号登录]
     */
    protected function smsLogin()
    {
        $phone = $this->_cget('phone');
        $code = $this->_cget('smscode');

        if (empty($phone)) {
            return $this->error("手机号不能为空！");
        }

        if (empty($code)) {
            return $this->error("请输入手机验证码！");
        }
        if (!isset($this->loginPhone[$phone])){
            return $this->error("账号异常,请联系管理员");
        }
        //校验手机验证码
        $service = new ApiSmsService();
        $res = $service->check($phone, $code, 0);
        if (!$res) {
            return $this->error($service->getError());
        }

        $userid = $this->loginPhone[$phone];
        $userinfo = array(
            'phone' => $phone,
            'userid' => $userid,
        );
        $userinfo['utoken'] = Validater::token($userid);
        unset($userinfo['password']);
        return $this->show($userinfo);
    }

    protected function loginSendSms(){
        $phone = $this->_cget('phone');
        if (!isset($this->loginPhone[$phone])){
            return $this->error("账号异常,请联系管理员");
        }
        $service = new ApiSmsService();
        $res = $service->send($phone, 1, 0);
        if ($res) {
            return $this->show(null, '发送成功');
        }
        return $this->error('短信发送失败');
    }
}
